package com.dogdog.activity;

import java.util.ArrayList;
import java.util.List;

import com.dogdog.fragment.Hand_Notification_evaluation;
import com.dogdog.fragment.Hand_Notification_found;
import com.dogdog.fragment.Hand_Notifications_post;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Hand_Notifications extends FragmentActivity {

	private ViewPager mViewPager;
	private FragmentPagerAdapter mAdapter;
	private List<Fragment> mFragments = new ArrayList<Fragment>();

	private TextView title;
	private TextView tv_post;
	private TextView tv_found;
	private TextView tv_tv_evaluation;
	private RelativeLayout titlebar;
	private ImageView mTabLine;
	private int screenWidth;
	private int currentIndex;
	private LinearLayout ly_post;
	private LinearLayout ly_found;
	private LinearLayout ly_evaluation;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.notifications);
		initView();
		initTabLine();

		mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

			@Override
			public int getCount() {
				return mFragments.size();
			}

			@Override
			public Fragment getItem(int arg0) {
				return mFragments.get(arg0);
			}
		};
		mViewPager.setAdapter(mAdapter);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				resetTextView();
				switch (arg0)	{
				case 0:
				    tv_post.setTextColor(getResources().getColor(R.color.theme));
					tv_found.setTextColor(getResources().getColor(R.color.black));
					tv_tv_evaluation.setTextColor(getResources().getColor(R.color.black));
					break;
				case 1:
					tv_post.setTextColor(getResources().getColor(R.color.black));
					tv_found.setTextColor(getResources().getColor(R.color.theme));
					tv_tv_evaluation.setTextColor(getResources().getColor(R.color.black));
					break;
				case 2:
					tv_post.setTextColor(getResources().getColor(R.color.black));
					tv_tv_evaluation.setTextColor(getResources().getColor(R.color.theme));
					tv_found.setTextColor(getResources().getColor(R.color.black));
					break;
				}
				currentIndex = arg0;
			}
			
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
				if (currentIndex == 0 && position == 0)// 0->1
				{
				LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine.getLayoutParams();
				lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 3) + currentIndex * (screenWidth / 3));
				mTabLine.setLayoutParams(lp);
				
			} else if (currentIndex == 1 && position == 0) // 1->0
			{
				LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine.getLayoutParams();
				lp.leftMargin = (int) (-(1 - positionOffset) * (screenWidth * 1.0 / 3) + currentIndex* (screenWidth / 3));
				mTabLine.setLayoutParams(lp);

			} else if (currentIndex == 1 && position == 1) // 1->2
			{
				LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
						.getLayoutParams();
				lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 3) + currentIndex * (screenWidth / 3));
				mTabLine.setLayoutParams(lp);
			} else if (currentIndex == 2 && position == 1) // 2->1
			{
				LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine.getLayoutParams();
				lp.leftMargin = (int) (-(1 - positionOffset) * (screenWidth * 1.0 / 3) + currentIndex* (screenWidth / 3));
				mTabLine.setLayoutParams(lp);
			}
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
		mViewPager.setCurrentItem(1);
	}

	private void initTabLine() {
		mTabLine = (ImageView) findViewById(R.id.id_tab_line);
		DisplayMetrics outMetrics = new DisplayMetrics();
		getWindow().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
		screenWidth = outMetrics.widthPixels;
		LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine.getLayoutParams();
		lp.width = screenWidth / 3;
		mTabLine.setLayoutParams(lp);
	}

	private void initView() {
		mViewPager = (ViewPager) findViewById(R.id.id_viewpager);

		titlebar = (RelativeLayout) findViewById(R.id.rl_titlebar);
		title = (TextView) findViewById(R.id.tv_title);
		title.setText("消息提醒");
		ly_post = (LinearLayout) findViewById(R.id.ly_post);
		ly_found = (LinearLayout) findViewById(R.id.ly_found);
		ly_evaluation = (LinearLayout) findViewById(R.id.ly_evaluation);

		ly_post.setOnClickListener(new MyOnClickListener(0));
		ly_found.setOnClickListener(new MyOnClickListener(1));
		ly_evaluation.setOnClickListener(new MyOnClickListener(2));
		
		tv_post = (TextView) findViewById(R.id.tv_post);
		tv_found = (TextView)findViewById(R.id.tv_found);
		tv_tv_evaluation = (TextView)findViewById(R.id.tv_evaluation);
		Hand_Notifications_post hand_Notifications_post = new Hand_Notifications_post();
		Hand_Notification_found found = new Hand_Notification_found();
		Hand_Notification_evaluation evaluation = new Hand_Notification_evaluation();
		mFragments.add(hand_Notifications_post);
		mFragments.add(found);
		mFragments.add(evaluation);

	}

	protected void resetTextView() {
		tv_post.setTextColor(getResources().getColor(R.color.black));
		tv_found.setTextColor(getResources().getColor(R.color.black));
		tv_tv_evaluation.setTextColor(getResources().getColor(R.color.black));
	}

	private class MyOnClickListener implements OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View arg0) {
			mViewPager.setCurrentItem(index);
		}
	}
}
